//package com.biu.billiards.mqtt;
//
//import com.biu.billiards.model.vo.mqtt.OpenDeviceVo;
//import com.biu.billiards.service.IBilliardsService;
//import com.biu.billiards.service.IDeviceService;
//import com.biu.billiards.service.IShopService;
//import com.biu.billiards.utils.RedisUtils;
//import org.json.JSONObject;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.integration.mqtt.support.MqttHeaders;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.MessageHandler;
//import org.springframework.messaging.MessageHeaders;
//import org.springframework.messaging.MessagingException;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.time.LocalTime;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
//@Slf4j
//@AllArgsConstructor
//@Component
//public class MqttMessageReceiver implements MessageHandler {
//
//
//    @Resource
//    private IBilliardsService billiardsService;
//
//
//    @Resource
//    private IDeviceService deviceService;
//
//
//    @Resource
//    private IShopService shopService;
//
//
//    @Resource
//    private RedisUtils redisUtils;
//
//    @Override
//    public void handleMessage(Message<?> message) throws MessagingException {
//        try {
//
//            MessageHeaders headers = message.getHeaders();
//            //获取消息Topic
//            String receivedTopic = (String) headers.get(MqttHeaders.RECEIVED_TOPIC);
//
//            log.info("[获取到的消息的topic :]{} ", receivedTopic);
//            //获取消息体
//            String payload = (String) message.getPayload();
//            log.info("[获取到的消息的payload :]{} ", payload);
//
//
//            if (shopService.getAutoStatus(receivedTopic)){
//
//                System.out.println("自动化打开的");
//
//              openDevice(receivedTopic);
//
//            }else {
//                System.out.println("自动化关闭的");
//            }
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    private void openDevice(String receivedTopic){
//
//
//       Boolean open = shopService.getAutoOpenStatus(receivedTopic);
//
//        System.out.println("open");
//
//
//      if (open){
//
//          redisUtils.set("close","1",5,TimeUnit.MINUTES);
//
//          System.out.println("有人在玩");
//         deviceService.openVrv(open);
//
//      }else {
//
//          System.out.println("没人在玩");
//          if (redisUtils.get("close")==null){
//              System.out.println("真正关闭");
//              deviceService.openVrv(open);
//          }
//
//
//      }
//
//
//
//
////       if (open){
////
////           System.out.println("当前有人在玩");
////         //  deviceService.openVrv(false);
////
////       }else {
////
////           System.out.println("没有人在玩");
////           deviceService.openVrv(true);
////
////       }
//
//
////        JSONObject jsonObject = new JSONObject(payload);
////
////        boolean allClosed = jsonObject.getInt("A03") == 0 && jsonObject.getInt("A04") == 0
////                && jsonObject.getInt("A05") == 0 && jsonObject.getInt("A06") == 0
////                && jsonObject.getInt("A07") == 0 ;
////
////
////        if (allClosed) {
////
////            System.out.println("没有人在玩");
////
////            if (jsonObject.getInt("A02") == 1 && jsonObject.getInt("A08") == 1 && jsonObject.getInt("A09") == 1 &&  redisUtils.get("close")==null){
////                System.out.println("10分钟之后关掉：音响，空调");
////                System.out.println("正在关闭，音响，空调");
////
////                //  deviceService.openVrv(0);
////               deviceService.openVrv(false);
////               // openVrv
////
////            }
////
////        } else {
////
////            System.out.println("当前有人在玩");
////
////            redisUtils.set("close","1",5,TimeUnit.MINUTES);
////
////            if (jsonObject.getInt("A02") == 0 || jsonObject.getInt("A08") == 0 || jsonObject.getInt("A09") == 0){
////
////                System.out.println("正在打开灯，音响，空调");
////
////               // deviceService.openVrv(true);
////
////                // deviceService.openVrv(1);
////
////            }
////
////
////        }
//
//
//
//    }
//
//
//}
